package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.manage.HttpSessionManage;
import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.service.StoreService;
import com.youi.entity.Store;
import com.youi.entity.Sysuser;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 门店信息控制层
* @author Administrator
*/
@Controller
public class StoreController  extends HttpRequestManage{

		@Autowired
		private StoreService storeService;

		/**
		 * I-60
		 * 添加或编辑门店信息
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/addOrEditStore")
		public String addOrEditStore(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Sysuser sysuser=(Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
			if (sysuser.getType()!=0) {
				return JsonUtil.toJson(CommonInfoUtil.NOPERMISSIONS, "无操作权限");
			}
			Long id=optLong("id",request);
			Store store=null;
			if (id!=null) {
				store=storeService.get(id);
			}else {
				store=new Store();
				store.setDelStatus(DelStatus.NODEL);
				store.setGmtCreated(new Date());
			}
			String name=optString("name",request);
			if (name!=null){
				store.setName(name);
			}
			String shortName=optString("shortName",request);
			if (shortName!=null){
				store.setShortName(shortName);
			}
			String code=optString("code",request);
			if (code!=null){
				store.setCode(code);
			}
			String phone=optString("phone",request);
			if (phone!=null){
				store.setPhone(phone);
			}
			String shopkeeper=optString("shopkeeper",request);
			if (shopkeeper!=null){
				store.setShopkeeper(shopkeeper);
			}
			String mobile=optString("mobile",request);
			if (mobile!=null){
				store.setMobile(mobile);
			}
			String province=optString("province",request);
			if (province!=null){
				store.setProvince(province);
			}
			String city=optString("city",request);
			if (city!=null){
				store.setCity(city);
			}
			String district=optString("district",request);
			if (district!=null){
				store.setDistrict(district);
			}
			String street=optString("street",request);
			if (street!=null){
				store.setStreet(street);
			}
			String address=optString("address",request);
			if (address!=null){
				store.setAddress(address);
			}
			String wgs84Lon=optString("wgs84Lon",request);
			if (wgs84Lon!=null){
				store.setWgs84Lon(wgs84Lon);
			}
			String wgs84Lat=optString("wgs84Lat",request);
			if (wgs84Lat!=null){
				store.setWgs84Lat(wgs84Lat);
			}
			String amapLon=optString("amapLon",request);
			if (amapLon!=null){
				store.setAmapLon(amapLon);
			}
			String amapLat=optString("amapLat",request);
			if (amapLat!=null){
				store.setAmapLat(amapLat);
			}
			String tenxunLon=optString("tenxunLon",request);
			if (tenxunLon!=null){
				store.setTenxunLon(tenxunLon);
			}
			String tengxunLat=optString("tengxunLat",request);
			if (tengxunLat!=null){
				store.setTengxunLat(tengxunLat);
			}
			String baiduLon=optString("baiduLon",request);
			if (baiduLon!=null){
				store.setBaiduLon(baiduLon);
			}
			String baiduLat=optString("baiduLat",request);
			if (baiduLat!=null){
				store.setBaiduLat(baiduLat);
			}
			Integer verify=optInteger("verify",request);
			if (verify!=null){
				store.setVerify(verify);
			}
			store.setGmtModify(new Date());
			storeService.saveOrUpdate(store);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 *批量删除门店信息
		*/
		@ResponseBody
		@RequestMapping("delStoreByIds")
		public String delStoreByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Store> storeList= storeService.getByParams(params);
			if (storeList==null||storeList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Store store : storeList) {
				store.setDelStatus(DelStatus.DEL);
			}
			storeService.saveOrUpdateAll(storeList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * I-58
		 * 查询门店信息
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getStore")
		public String getStore(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Sysuser sysuser=(Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
			if (sysuser.getType()!=0) {
				return JsonUtil.toJson(CommonInfoUtil.NOPERMISSIONS, "无操作权限");
			}
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			String des=optString("des",request,Common.DESC);
			String storepms=optString("storepms",request);
			String managerpms=optString("managerpms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> storeParams=null;
			if (storepms!=null&&storepms.length()>0) {
				storeParams=new HashMap<String, Object>();
				storeParams.put("name", storepms);
				storeParams.put("code", storepms);
			}
			Map<String, Object> managerParams=null;
			if (managerpms!=null&&managerpms.length()>0) {
				managerParams=new HashMap<String, Object>();
				managerParams.put("shopkeeper", managerpms);
				managerParams.put("mobile", managerpms);
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Store> storeList=storeService.getByStoreList(prams, storeParams, managerParams, sortPram, (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (storeList==null||storeList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=storeService.getStoreCount(prams, storeParams, managerParams, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, storeList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 * I-59
		 * 查询门店信息
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getStoreById")
		public String getStoreById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Sysuser sysuser=(Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
			if (sysuser.getType()!=0) {
				return JsonUtil.toJson(CommonInfoUtil.NOPERMISSIONS, "无操作权限");
			}
			Long id=getLong("id",request);
			Store store=storeService.get(id);
			if (store==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",store);//jsonObject	json对象
		}

		/**
		 * 查询所有门店信息
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("getAllStore")
		public String getAllStore(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			List<Store> storeList= storeService.getByParams(null);
			if (storeList==null||storeList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",storeList);//jsonObjectList	json对象集合
		}
}
